import com.alibaba.fastjson.JSONObject;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import site.abely.util.UrlUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class EngineActivity {

    public static void main(String[] args) throws IOException {
//        manual(10, "http://engine.lvehaisen.com/index/activity?appKey=2RRspaDTHUuQm3anqFgYobtu7LNM&adslotId=6574");
        sdk(200, 5457L, "3F428C1FH8dmnaFisMvmMr6zW2eR");
    }

    public static void manual(int 循环次数, String 请求地址) throws IOException {
        int max = 循环次数;
        String url = 请求地址;
        Set<String> ids = new HashSet<>();
        Map<String, String> cookies = new HashMap<>();
        for (int i = 0; i < max; i++) {
            Connection.Response response = Jsoup.connect(url).cookies(cookies).ignoreContentType(true).execute();
            if (cookies.isEmpty()) {
                cookies = response.cookies();
            }
//            System.out.println(response.url().toString());
            String id = UrlUtils.getParamValue(response.url(), "id");
            if (ids.contains(id)) {
                System.out.println();
                ids.clear();
            }
            ids.add(id);
            System.out.print(id + " ");
        }
    }

    public static void sdk(int 循环次数, Long 广告位ID, String appKey) throws IOException {
        int max = 循环次数;
//        String url = "http://engine.lvehaisen.com/api/v1/activity/get";
        String url = "http://engine.tuia.cn/api/v1/activity/get";
        Map<String,String> map = new HashMap<>();
        map.put("adslot_id", String.valueOf(广告位ID));
        map.put("app_key", appKey);
        map.put("api_version","1.0.0");
        map.put("device_id","wvOTzEoS0BgRJEsLwfsdfafMK14908469055asdfafa5411024");
        map.put("ipv4","172.16.29.38");
        map.put("request_id","G1xL5D4Hzr7kxPKKB2XP9Hu1qefertwertL2HK8JZRKNRagc0MocYx1490923903115");
        Set<String> ids = new HashSet<>();
        url = UrlUtils.appendParams(url,map);
        Map<String, String> cookies = new HashMap<>();
        Map<String, Integer> pec = new HashMap<>();
        for (int i = 0; i < max; i++) {
            Connection.Response response = Jsoup.connect(url).cookies(cookies).ignoreContentType(true).execute();
            if (cookies.isEmpty()) {
                cookies = response.cookies();
            }
            JSONObject rspJson = JSONObject.parseObject(response.body());
                        String id = rspJson.getString("activity_id");
                        String source = rspJson.getString("source");
                        String putWay = rspJson.getString("activity_way");
                        if (id.contains(",")) {
                            id = id.split(",")[0];
                            id = id.substring(1, id.length());
                            id = String.valueOf(Long.valueOf(id));
                        }
                        if (ids.contains(id)) {
                            System.out.println();
                            ids.clear();
                        }
                        ids.add(id);
            System.out.print(id + ":" + source +"-"+putWay+" ");
//            String s = rspJson.getString("activity_way");
//            pec.merge(s, 1, (a, b) -> a + b);
        }
//        pec.forEach((k, v) -> {
//            System.out.println(k + ":" + v);
//        });
    }
}
